﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Nova_DAL
{
    public class RF
    {
        int i = 0;
        string strSQL = "";
        //DataSet ds = null;
        DbHelper dbHelper = new DbHelper();

        /// <summary>
        /// 获取托盘组盘的物料信息
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="ds"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:获取数据失败 0：无数据 1：获取数据成功</returns>
        public int GetTrayMaterialInfor(string strTrayId, out DataSet ds, out string strMessage)
        {
            strMessage = "";
            ds = null;
            try 
            {
                strSQL = string.Format(@"SELECT TBI.MAT_CODE 物料编码,TBM.MAT_NAME 物料名,convert(int,TBS.Mat_Num) 数量,TBM.S01 规格,TBM.Unit_Base 单位 FROM TRelations TRP 
                  INNER JOIN TBase_Position TBP ON TRP.POS_ID=TBP.POS_ID
                  INNER JOIN TBase_Zone TBZ ON TBZ.ZONE_ID=TBP.ZONE_ID
                  INNER JOIN TBase_TrayPos TBT ON TBT.TRAY_ID = TRP.TRAY_ID
                  INNER JOIN TRel_TrayPosContainer TRT ON TRT.RELATIONS_ID=TBT.RELATIONS_ID
                  INNER JOIN TBase_Storage TBS ON TBS.CONTAINERID=TRT.CONTAINERID
                  INNER JOIN TBase_Interface TBI ON TBI.SEC_MAT_ID=TBS.SEC_MAT_ID
                  INNER JOIN TBase_Material TBM ON TBM.MAT_CODE=TBI.MAT_CODE
                  WHERE  TRP.TRAY_ID = @strTrayId");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@strTrayId", DbType.String, strTrayId);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        strMessage = "获取数据成功";
                        return 1;
                    }
                    else
                    {
                        strMessage = "此托盘上没有物料信息数据";
                        return 0;
                    }
                }
                
                 strMessage = "此托盘上没有物料信息数据";
                 return 0;
                
            }
            catch (Exception ex)
            {
                strMessage = "获取数据异常";

                return -1;
            }
        }

        public int GetTrayMaterialInforNF(string strTrayId, out DataSet ds, out string strMessage)
        {
            strMessage = "";
            ds = null;
            try
            {
                strSQL = string.Format(@"SELECT (case when TPT.StockNo is null then '' else TPT.StockNo end)单号 ,TBM.MAT_NAME 物料名,convert(int,TBS.Mat_Num) 数量,TBM.S01 规格,TBM.Unit_Base 单位,TBI.MAT_CODE 物料编码 
                  FROM TRelations TRP 
                  INNER JOIN TBase_Position TBP ON TRP.POS_ID=TBP.POS_ID
                  INNER JOIN TBase_Zone TBZ ON TBZ.ZONE_ID=TBP.ZONE_ID
                  INNER JOIN TBase_TrayPos TBT ON TBT.TRAY_ID = TRP.TRAY_ID
                  INNER JOIN TRel_TrayPosContainer TRT ON TRT.RELATIONS_ID=TBT.RELATIONS_ID
                  INNER JOIN TBase_Storage TBS ON TBS.CONTAINERID=TRT.CONTAINERID
                  INNER JOIN TBase_Interface TBI ON TBI.SEC_MAT_ID=TBS.SEC_MAT_ID
                  INNER JOIN TBase_Material TBM ON TBM.MAT_CODE=TBI.MAT_CODE
                  Left JOIN TPackage_tail TPT  ON  TPT.TBStorage_PID=TBS.PID
                  WHERE  TRP.TRAY_ID = @strTrayId");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@strTrayId", DbType.String, strTrayId);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        strMessage = "获取数据成功";
                        return 1;
                    }
                    else
                    {
                        strMessage = "此托盘上没有物料信息数据";
                        return 0;
                    }
                }

                strMessage = "此托盘上没有物料信息数据";
                return 0;

            }
            catch (Exception ex)
            {
                strMessage = "获取数据异常";

                return -1;
            }
        }



        /// <summary>
        /// 根据物料编码获取物料的信息
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="ds"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:获取数据失败 0：无数据 1：获取数据成功</returns>
        public int GetMaterialInfor(string materialCode, out DataSet ds, out string strMessage)
        {
            strMessage = "";
            ds = null;
            try
            {
                strSQL = string.Format(@"select * from TBase_Material where Mat_Code = @materialCode");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@materialCode", DbType.String, materialCode);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        strMessage = "获取数据成功";
                        return 1;
                    }
                    else
                    {
                        strMessage = "没有获取到物料信息";
                        return 0;
                    }
                }

                strMessage = "没有获取到物料信息";
                return 0;

            }
            catch (Exception ex)
            {
                strMessage = "获取数据异常";

                return -1;
            }
        }


        /// <summary>
        /// 托盘是否允许组盘
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:异常失败 0：不允许 1：允许</returns>
        public int TrayCanPackage(string strTrayId, out string strMessage)
        {
            strMessage = "";
            
            try
            {

                strSQL = string.Format(@"SELECT TOP 1 Structure FROM TRelations TRP 
			                    LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
			                    LEFT JOIN TBase_Zone TBZ ON TBZ.Zone_ID = TBP.Zone_ID 
			                    WHERE TRP.TRAY_ID = @TRAY_ID");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTrayId);
                DataSet ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "0")
                    {
                        strMessage = "该托盘已经在立体库，不允许再次组盘！";
                        return 0;
                    }
                   
                }

               
                return 1;

            }
            catch (Exception ex)
            {
                strMessage = "查询数据异常";

                return -1;
            }
        }


        /// <summary>
        ///  物料编码是否允许组盘
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:异常失败 0：不允许 1：允许</returns>
        public int MaterialCanPackage(string materialCode, out string strMessage)
        {
            strMessage = "";
            try
            {
                strSQL = string.Format(@"select * from TBase_Material where Mat_Code = @Mat_Code");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, materialCode);
                DataSet ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {   
                    return 1;
                }

                strMessage = "物料表中没有此物料编码，请在物料表中先添加此物料信息";
                return 0;
            }
            catch (Exception ex)
            {
                strMessage = "执行操作异常";
                return -1;
            }
        }

      


        /// <summary>
        /// 根据物料编码获取物料内部编号
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="SecMatId"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:失败 1：成功</returns>
        public int GetSecMatIdByMateCode(string materialCode,out long SecMatId, out string strMessage)
        {
            strMessage = "";
            SecMatId = 0;
            try
            {

                DbCommand cmd = dbHelper.GetStoredProcCommond("SP_Get_Sec_Mat_ID");
                dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, materialCode);
                dbHelper.AddInParameter(cmd, "@D01", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D02", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D03", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D04", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D05", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D06", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D07", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D08", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D09", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@D10", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S01", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S02", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S03", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S04", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S05", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S06", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S07", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S08", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S09", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S10", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S11", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S12", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S13", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S14", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S15", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S16", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S17", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S18", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S19", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@S20", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@N01", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N02", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N03", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N04", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N05", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N06", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N07", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N08", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N09", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@N10", DbType.Decimal, 0);
                dbHelper.AddInParameter(cmd, "@DynamicState01", DbType.Int16, 0);
                dbHelper.AddInParameter(cmd, "@DynamicState02", DbType.Int16, 0);
                dbHelper.AddInParameter(cmd, "@DynamicState03", DbType.Int16, 0);
                dbHelper.AddInParameter(cmd, "@DynamicState04", DbType.Int16, 0);
                dbHelper.AddInParameter(cmd, "@DynamicState05", DbType.Int16, 0);
                dbHelper.AddInParameter(cmd, "@TS01", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@TS02", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@TS03", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@TS04", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@TS05", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@ForceNew", DbType.Int16, 1);
                dbHelper.AddInParameter(cmd, "@UserName", DbType.String, "");
                //dbHelper.AddInParameter(cmd, "@UserName", DbType.String, "RFClient");
                dbHelper.AddOutParameter(cmd, "@Sec_Mat_ID", DbType.Int32, 20);
                dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int16, 10);
                dbHelper.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
                dbHelper.ExecuteNonQuery(cmd);
                int result =Convert.ToInt32(dbHelper.GetParameter(cmd, "@ResNO").Value);
                strMessage = dbHelper.GetParameter(cmd, "@ResMsg").Value.ToString();
                if (result==1)
                {
                    SecMatId = Convert.ToInt64(dbHelper.GetParameter(cmd, "@Sec_Mat_ID").Value);
                }
                return result;
            }
            catch (Exception ex)
            {
                strMessage = "执行操作异常";
                return -1;
            }

            
           
        }

        /// <summary>
        /// 执行组盘存储过程
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="SecMatId"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int Package(string strTrayId, long SecMatId,int MatNum, out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_PACKAGE_ALL_100X");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTrayId);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64, SecMatId);
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int32, MatNum);
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;
        }

        /// <summary>
        /// 执行组盘存储过程--含尺寸、重量类型
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="SecMatId"></param>
        /// <param name="MatNum"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int PackageCommon(string strTrayId, long SecMatId, int MatNum,int TypeOfSize,int TypeOfWeight, out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_PACKAGE_ALL_FOR_COMMON");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTrayId);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64, SecMatId);
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int32, MatNum);

            dbHelper.AddInParameter(cmd, "@TypeOfSize", DbType.Int32, TypeOfSize);
            dbHelper.AddInParameter(cmd, "@TypeOfWeight", DbType.Int32, TypeOfWeight);

            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;
        }

        /// <summary>
        /// 执行组盘存储过程-绑定订单单号的
        /// </summary>
        /// <param name="strStockNo"></param>
        /// <param name="strMat_Code"></param>
        /// <param name="strTrayId"></param>
        /// <param name="SecMatId"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int PackageCommonNF(string strStockNo,string strMat_Code, string strTrayId, long SecMatId, int MatNum, out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_PACKAGE_ALL_100X_NF");
            dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, strStockNo);
            dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, strMat_Code);
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTrayId);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64, SecMatId);
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int32, MatNum);
            dbHelper.AddInParameter(cmd, "@Act_Num", DbType.Decimal, Convert.ToDecimal(MatNum));
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);

            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;
        }



        /// <summary>
        /// 获取存储键值数据
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="ContainerID"></param>
        /// <param name="Sec_Mat_ID"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int GetStorageKey(string strTrayId, string materialCode, out string ContainerID, out long Sec_Mat_ID, out string strMessage)
        {
            strMessage = "";
            ContainerID = "";
            Sec_Mat_ID = 0;
            try
            {
                strSQL = string.Format(@"
                  Select tbs.ContainerID,tbs.Sec_Mat_ID from TBase_Storage tbs 
                  inner join TBase_Interface tbi on tbs.Sec_Mat_ID=tbi.Sec_Mat_ID
                  inner join TRel_TrayPosContainer tr on tbs.ContainerID=tr.ContainerID
                  inner join TBase_TrayPos ttp on ttp.Relations_ID=tr.Relations_ID
                  where tbi.Mat_Code=@Mat_Code and ttp.Tray_ID=@Tray_ID
                  ");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, materialCode);
                dbHelper.AddInParameter(cmd, "@Tray_ID", DbType.String, strTrayId);
                DataSet ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    ContainerID = ds.Tables[0].Rows[0]["ContainerID"].ToString();
                    Sec_Mat_ID = Convert.ToInt64(ds.Tables[0].Rows[0]["Sec_Mat_ID"]);
                    return 1;
                }

                strMessage = "没有获取到组盘的存储键值数据";
                return 0;
            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }

        /// <summary>
        /// 更新组盘物料存储数
        /// </summary>
        /// <param name="ContainerID"></param>
        /// <param name="Sec_Mat_ID"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:失败；1：成功</returns>
        public int UpdateStorageNum(string ContainerID,long Sec_Mat_ID,int MatNum, out string strMessage)
        {
            strMessage = "";
            try
            {
                strSQL = string.Format(@"
                update TBase_Storage set Mat_Num = @Mat_Num where ContainerID = @ContainerID and Sec_Mat_ID = @Sec_Mat_ID
                  ");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@Mat_Num", DbType.Int32, MatNum);
                dbHelper.AddInParameter(cmd, "@ContainerID", DbType.String, ContainerID);
                dbHelper.AddInParameter(cmd, "@Sec_Mat_ID", DbType.Int64, Sec_Mat_ID);
                i = dbHelper.ExecuteNonQuery(cmd);
                if (i <= 0)
                {
                    strMessage = "更新失败";
                    return -1;
                }

                return 1;
            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }

        /// <summary>
        /// 更新组盘物料存储数NF
        /// </summary>
        /// <param name="ContainerID"></param>
        /// <param name="Sec_Mat_ID"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int UpdateStorageNumNF(string StockNo,string ContainerID, long Sec_Mat_ID, int MatNum, out string strMessage)
        {

            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_UPDATE_PACKAGE_NUMBER_NF");
            dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
            dbHelper.AddInParameter(cmd, "@ContainerID", DbType.String, ContainerID);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, Sec_Mat_ID.ToString());
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int32, MatNum);
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);

            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;

        }



        


        /// <summary>
        /// 执行组盘删除
        /// </summary>
        /// <param name="ContainerID"></param>
        /// <param name="SecMatId"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败；1：成功</returns>
        public int PackageDel(string ContainerID, long SecMatId, int MatNum, out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_UNPACKAGE_ALL_0000");
            dbHelper.AddInParameter(cmd, "@ContainerID", DbType.String, ContainerID);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, SecMatId.ToString());
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int32, MatNum);
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;
        }

        public int PackageDelNF(string StockNo, string ContainerID, long SecMatId, int MatNum, out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_UNPACKAGE_ALL_0000_NF");
            dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
            dbHelper.AddInParameter(cmd, "@ContainerID", DbType.String, ContainerID);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, SecMatId.ToString());
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int32, MatNum);
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;
        }

        public int PackageDelNF_New(string StockNo, string ContainerID, long SecMatId, int MatNum, out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("dbo.SP_UNPACKAGE_ALL_0000_NF_0220");
            dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
            dbHelper.AddInParameter(cmd, "@ContainerID", DbType.String, ContainerID);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, SecMatId.ToString());
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int32, MatNum);
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;
        }


        /// <summary>
        /// 根据托盘码获取托盘尺寸类型
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:获取数据失败 0：无数据 1：获取数据成功</returns>
        public int GetSizeByTrayId(string strTrayId, out int TypeOfSize, out string strMessage)
        {
            strMessage = "";
            TypeOfSize = 0;
            try
            {
                strSQL = string.Format(@"select TypeOfSize from TRelations where Tray_ID=@strTrayId");
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@strTrayId", DbType.String, strTrayId);
                DataSet ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        TypeOfSize = Convert.ToInt32(ds.Tables[0].Rows[0]["TypeOfSize"]);
                        strMessage = "获取数据成功";
                        return 1;
                    }
                    else
                    {
                        strMessage = "没有获取到物料信息";
                        return 0;
                    }
                }

                strMessage = "没有获取到物料信息";
                return 0;

            }
            catch (Exception ex)
            {
                strMessage = "获取数据异常";

                return -1;
            }
        }


        /// <summary>
        /// 获取入库单据上物料剩余数量
        /// </summary>
        /// <param name="StockNo"></param>
        /// <param name="MatCode"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int GetMaterialReNum(string StockNo, string MatCode, out string strMessage)
        {
            int Num = 0;
            strMessage = "";

            try
            {
                strSQL = @"SELECT(Num - Act_Num) as ReNum   FROM TBill_AddStorage TBA
                            LEFT JOIN TAddStorage_List TAL ON TAL.StockNo = TBA.StockNo
                            WHERE TBA.StockNo = @StockNo and Mat_Code =@Mat_Code";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
                dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, MatCode);
                DataSet ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Num = Convert.ToInt32(ds.Tables[0].Rows[0]["ReNum"]);
                        strMessage = "获取数据成功";
                        return Num;
                    }
                    else
                    {
                        strMessage = "没有获取到物料信息";
                        return -1;
                    }
                }

                strMessage = "没有获取到物料信息";
                return -1;

            }
            catch (Exception ex)
            {
                strMessage = "获取数据异常";

                return -1;
            }
        }

        /// <summary>
        /// 获取未处理入库单据列表
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public DataSet DataListStorage()
        {
            DataSet ds = null;
            try
            {
                strSQL = @"SELECT top 6 TBA.StockNo,MAX(CONVERT(varchar(100), TBA.CDT, 23))AS CDT FROM TBill_AddStorage TBA LEFT JOIN TAddStorage_List TAL ON TAL.StockNo = TBA.StockNo
                           WHERE TBA.Bill_Status = 0  GROUP BY TBA.StockNo ORDER BY CDT DESC";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);

                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }


        /// <summary>
        /// 获取单据物料信息
        /// </summary>
        /// <param name="StorageNo"></param>
        /// <returns></returns>
        public DataSet DataListMaterial(string StockNo)
        {
            DataSet ds = null;
            try
            {
                strSQL = @" SELECT TAL.Mat_Code,Mat_Name FROM TBill_AddStorage TBA 
                            LEFT JOIN TAddStorage_List TAL ON TAL.StockNo = TBA.StockNo
                            LEFT JOIN TBase_Material TBM ON TAL.Mat_Code=TBM.Mat_Code 
                            WHERE TBA.Bill_Status = 0  and TBA.StockNo=@StockNo ORDER BY TAL.CDT DESC";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }








        /// <summary>
        /// pda 根据托盘号 获取出库指令展示信息 
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="ds"></param>
        /// <param name="strMessage"></param>
        /// <returns>1：获取到值 0：没有获取到值 -1：执行异常</returns>
        public int GetPDAShow(string strTrayId, out DataSet ds, out string strMessage)
        {
            strMessage = "";
            ds =new DataSet();
            try
            {
                DbCommand cmd = dbHelper.GetStoredProcCommond("SP_PDAShow");
                dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTrayId);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null && (ds.Tables.Count>0))  // 与一般的SQL获取值不同，存储过程没有走到查询值跳出的，ds!=null 但是ds 中无Tables，会导致ds.Tables[0].Rows.Count 异常直接跳出
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        strMessage = "获取出库指令信息成功";
                        return 1;
                    }
                    else
                    {
                        strMessage = "此托盘号没有获取出库指令信息";
                        return 0;
                    }
                }

                strMessage = "此托盘号没有获取出库指令信息";
                return 0;

            }
            catch
            {
                strMessage = "获取数据异常";

                return -1;
            }
                
              
        }

        /// <summary>
        /// PDA 过账完成
        /// </summary>
        /// <param name="TaskID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败；1：成功</returns>
        public int PDAUpdateCMDS(long TaskID,  out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_Update_CMDs");
            dbHelper.AddInParameter(cmd, "@TaskID", DbType.Int64, TaskID);
            dbHelper.AddInParameter(cmd, "@XTYPE", DbType.Int16, 1);  //1：完成
            dbHelper.AddInParameter(cmd, "@HType", DbType.Int16, 1);  //1:pda过账
            dbHelper.AddInParameter(cmd, "@UserName", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
            return iResNo;
        }


        /// <summary>
        /// 设置空托盘的尺寸等信息
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <param name="TypeOfZone"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败；1：成功</returns>
        public int SetSpaceTrayInfor(string strTrayId, int TypeOfSize, int TypeOfWeight,int TypeOfZone, out string strMessage)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("SP_SET_SPACE_TRAY_INFOR");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTrayId);

            dbHelper.AddInParameter(cmd, "@TypeOfSize", DbType.Int32, TypeOfSize);
            dbHelper.AddInParameter(cmd, "@TypeOfWeight", DbType.Int32, TypeOfWeight);
            dbHelper.AddInParameter(cmd, "@TypeOfZone", DbType.Int32, TypeOfZone);

            dbHelper.AddInParameter(cmd, "@UserName", DbType.String, "RFClient");
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@ResNO").Value.ToString());
            strMessage = dbHelper.GetParameter(cmd, "@ResMsg").Value.ToString();
            return iResNo;
        }

        /// <summary>
        /// PDA产生入库指令
        /// </summary>
        /// <param name="TrayId"></param>
        /// <param name="SQIDS"></param>
        /// <param name="TaskID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:失败 1：成功</returns>
        public int PDAGenerateInputCmd(string TrayId, Int16 SQIDS,out long TaskID, out string strMessage)
        {
            TaskID = 0;
            strMessage = "";
            int iResNo;
            try
            {
                DbCommand cmd = dbHelper.GetStoredProcCommond("SP_Generate_Input_CMDS_By_TRay_ID_100X");
                dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, TrayId);
                dbHelper.AddInParameter(cmd, "@DEFAULT_TYPEOFSIZE", DbType.Int16, 0);
                dbHelper.AddInParameter(cmd, "@DEFAULT_TYPEOFWEIGHT", DbType.Int16, 0);
                dbHelper.AddInParameter(cmd, "@SQIDS", DbType.Int16, SQIDS);
                dbHelper.AddInParameter(cmd, "@MEMO", DbType.String, "");
                dbHelper.AddInParameter(cmd, "@User_Name", DbType.String, "RFClient");

                dbHelper.AddOutParameter(cmd, "@TASKID", DbType.Int64, 100);
                dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int16, 100);
                dbHelper.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
                dbHelper.ExecuteNonQuery(cmd);

                
                iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@ResNO").Value.ToString());
                strMessage = dbHelper.GetParameter(cmd, "@ResMsg").Value.ToString();
                if(iResNo==1)
                {
                    TaskID = Convert.ToInt64(dbHelper.GetParameter(cmd, "@TASKID").Value.ToString());
                }
               
            }
            catch(Exception ex)
            {
                strMessage = "PDA产生入库指令方法异常跳出";
                iResNo = -1;
            }
            return iResNo;
        }



    }
}
